package cn.edu.nju.ws.sview;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.views.Entity;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;

/**
 * manage data in table visit_logs
 * @author Qingxia Liu
 *
 */
public class LogManagement {
	public static final String EVENT_LOGIN = "login";
	public static final String EVENT_LOGOUT = "logout";
	public static final String EVENT_VISIT = "visit";
	
	//Entity.CUSTOM_ENTITY|URI_ENTITY|COLLECTION
	//DatasetItem.URI_INT|CUSTOM_INT
	/**
	 * check if item_type is legal
	 * @param item_type
	 * @return true==isLegal,false==isNotLegal
	 */
	public static boolean checkItemType(int item_type){
		boolean legal = false;
		if(item_type==Entity.CUSTOM_ENTITY || item_type==Entity.URI_ENTITY || item_type==Entity.COLLECTION 
				|| item_type==DatasetItem.URI_INT || item_type==DatasetItem.CUSTOM_INT){
			legal = true;
		}
		return legal;
	}
	/**
	 * check if event_type is legal
	 * @param event_type
	 * @return true==isLegal,false==isNotLegal
	 */
	public static boolean checkEventType(String event_type){
		boolean legal = false;
		if(event_type.contentEquals(EVENT_LOGIN) || event_type.contentEquals(EVENT_LOGOUT) 
				|| event_type.contentEquals(EVENT_VISIT)){
			legal = true;
		}
		return legal;			
	}
	/**
	 * log visit info
	 * @param userId
	 * @param item_type
	 * @param item_id
	 * @param view_id
	 * @param lang
	 */
	public static void storeLogVisit(int userId, int item_type, int item_id, int view_id, String lang){
//		System.out.println("storeLogVisit:"+userId+"|"+item_type+"|"+item_id+"|"+view_id+"|"+lang);
		//check item_type
		if(!checkItemType(item_type)){
			throw new IllegalArgumentException();
		}
		//store
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("INSERT IGNORE INTO visit_logs(user_id, event_type, item_type, item_id, view_id, visit_time, lang) " +
					" VALUES(?,?,?,?,?,now(),?)");
			pstmt.setInt(1, userId);
			pstmt.setString(2, EVENT_VISIT);
			pstmt.setInt(3, item_type);
			pstmt.setInt(4, item_id);
			pstmt.setInt(5, view_id);
			pstmt.setString(6, lang);
			pstmt.execute();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * log user login or logout event info
	 * @param userId
	 * @param event_type
	 * @param lang
	 */
	public static void storeLogInOut(int userId, String event_type, String lang){
		//check event_type
		if(!checkEventType(event_type)){
			throw new IllegalArgumentException();
		}
		//store
		try {
			Connection conn = DBConnectionFactory.getConnection();
			PreparedStatement pstmt = conn.prepareStatement("INSERT IGNORE INTO visit_logs(user_id, event_type, visit_time, lang) " +
					" VALUES(?,?,now(),?)");
			pstmt.setInt(1, userId);
			pstmt.setString(2, event_type);
			pstmt.setString(3, lang);
			pstmt.execute();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}
